<?php
namespace Index\Controller;

class IndexController extends SystemController {

    public function _initialize(){
        parent::_initialize();
        $this->news_cate = D('Category');
        $this->article=D('Article');
    }

	public function index() {
		$this -> display();
	}


    //新闻内容列表
    public function news(){
        $cate_id = isset($_GET['cate_id'])?array('cate_id'=>$_GET['cate_id']):'';
        $this->news_cate = $this->news_cate->select(); //读取新闻栏目，这里需要配置新闻栏目ID

        $count = $this->article -> where(array('alt_del_stats'=>0))-> count();

        $page = new \Think\Page($count, 8);
        // 实例化分页类 条数，分页数
        $limit = $page -> firstRow . ',' . $page -> listRows;
        $this -> news  = $this->article->where(array('alt_del_stats'=>0))->limit($limit) ->select();
        $this -> page = $page -> show();
        //p($this->news_cate);
        $this ->display('xinwen');
    }
    
    
    //新闻查看

    public function show(){
        $art_id = $_GET['id'];
        //查找当前ID的新闻
        $this->new = $this->article->where(array('art_id'=>$art_id))->find();
        //读取上一篇
        $this->pre = $this->article->where(array('alt_del_stats'=>0))->where(array('art_id'=>array('lt',$art_id)))->order('art_id desc')->find();
        //读取下一篇
        $this->next = $this->article->where(array('alt_del_stats'=>0))->where(array('art_id'=>array('gt',$art_id)))->order('art_id desc')->find();
        //echo M()->getLastSql();
        $this->display('show');
    }

    public function message(){
        if(IS_POST){
            $data=array(
                'msg_title'=>I('msg_title'),
                'msg_username'=>I('msg_username'),
                'msg_mobile'=>I('msg_mobile'),
                'msg_sex'=>I('msg_sex'),
                'msg_qq'=>I('msg_qq'),
                'msg_email'=>I('msg_email'),
                'msg_content'=>I('msg_content'),
                'msg_time'=>time()
            );
            $res = M('message')->add($data);
            if($res){
                $data =[
                    'status'=>1,
                    'info'=>'留言提交成功！',
                ];
            }else{
                $data = [
                    'status'=>0,
                    'info'=>'留言提交失败，请稍后再试！',
                ];
            }
            $this->ajaxReturn($data);
        }else{
            $this->display();
        }
    }

}
